//
//  HRMineViewController.m
//  Hear
//
//  Created by mac on 2017/6/8.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRMineViewController.h"
#import "HRCollectionViewModel.h"
#import "HRMineButtonCell.h"
#import "HRMineHeadView.h"
#import "HRGetUserinfoViewModel.h"
#import "HRUpdataUserInfoModel.h"
#import "HREditUserInfoViewController.h"
#import "HRMessageRootViewController.h"
#import "HRFMDBDynamicNotifacationTool.h"

@interface HRMineViewController ()
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)HRCollectionViewModel *collectionViewModel;
@property (nonatomic, strong)HRMineHeadView *headView;
@property (nonatomic, strong)HRGetUserinfoViewModel *getUserInfoModel;
@property (nonatomic, assign)CGFloat headViewHeight;

@property (nonatomic, strong)UILabel *badgeView;

@end


@implementation HRMineViewController

#pragma mark - Get and Set
-(HRGetUserinfoViewModel *)getUserInfoModel{
    if (_getUserInfoModel == nil){
        _getUserInfoModel = [HRGetUserinfoViewModel new];
    }
    return _getUserInfoModel;
}

-(HRMineHeadView *)headView{
    if (!_headView){
        WeakSelf
        _headView = [[HRMineHeadView alloc] initWithFrame:CGRectMake(0, kTopHeight, kFullWidth, 115)];
        self.headViewHeight = _headView.height;
        _headView.clickIconBlock = ^(){
            HREditUserInfoViewController *enditVC = [HREditUserInfoViewController new];
            enditVC.infoModel = weakSelf.headView.userInfoModel;
            enditVC.hearIdStr = weakSelf.headView.userInfoModel.UserNo;
            [weakSelf.navigationController pushViewController:enditVC animated:YES];
        };
    }
    return _headView;
}

-(HRCollectionViewModel *)collectionViewModel{
    if (!_collectionViewModel){
        
        _collectionViewModel = [HRCollectionViewModel new];
        _collectionViewModel.scrollViewDidScrollBlock = ^(CGFloat offsetY){
//            [weakSelf scrollViewDidScroll:offsetY];
        };
    }
    return _collectionViewModel;
}

- (void)scrollViewDidScroll:(CGFloat)offsetY{
    if (offsetY < -self.headViewHeight) {
        self.headView.top = offsetY;
        self.headView.height = -offsetY;
        self.headView.scrollView.frame = self.headView.bounds;
        
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor= [UIColor whiteColor];
    
    self.title = @"我的";
    
    [self setupView];
    
    [self getUserInfo];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getUserInfo) name:@"reloadIconAndBackGroundNotififation" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newMesageNotifi) name:@"newMesageNotifi" object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTabItemBageCountNotifi) name:@"reloadTabItemBageCountNotifi" object:nil];

}

- (void)setupView{
    
    [self.view addSubview:self.headView];

    UIButton *messageButton = [UIButton buttonWithType:0];
    [messageButton setImage:[UIImage imageNamed:@"消息"] forState:0];
    messageButton.size = CGSizeMake(30, 30);
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:messageButton];
//    [messageButton addTarget:self action:@selector(messageButtonClick) forControlEvents:UIControlEventTouchUpInside];
    UILabel *badgeView = [UILabel new];
    badgeView.backgroundColor = CustomColor(250, 26, 41, 1);
    [messageButton  addSubview:(self.badgeView=badgeView)];
    badgeView.frame = CGRectMake(messageButton.width-10, 0, 8, 8);
    badgeView.layer.cornerRadius = badgeView.width/2;
    badgeView.clipsToBounds = YES;
    badgeView.hidden = YES;
    
    
    CGFloat leftPading = 16;
    
    CGFloat cellW = (kFullWidth-leftPading*5)/4;
    
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    // 设置cell的大小
    layout.itemSize = CGSizeMake(cellW, cellW);
    // 设置cell的间距
    layout.minimumLineSpacing = leftPading;
    layout.minimumInteritemSpacing = leftPading;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(leftPading, self.headView.bottom+leftPading, kFullWidth-leftPading*2, kFullHeight) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
//    self.collectionView.scrollEnabled = NO;
    [self.collectionView registerClass:[HRMineButtonCell class] forCellWithReuseIdentifier:@"HRMineButtonCell"];

    [self.collectionViewModel handleWithCollection:self.collectionView];
    [self.view addSubview:self.collectionView];
    self.collectionViewModel.contentArray = @[@"我的主页",@"设置",@"新手引导",@"意见反馈"];
}

- (void)messageButtonClick{
    HRMessageRootViewController *VC = [HRMessageRootViewController new];
    [self.navigationController pushViewController:VC animated:YES];
}


- (void)getUserInfo{
    WeakSelf
    [self.getUserInfoModel getUserInfoRequestModel:@{@"CurrentUserLocalId":USERID,@"VisitedUserLocalId":USERID} isSeccess:^(HRUpdataUserInfoModel *infModel) {
        dispatch_async(dispatch_get_main_queue(), ^{
           weakSelf.headView.userInfoModel = infModel;
            weakSelf.collectionViewModel.userInfoModel = infModel;
            if ([infModel.IsShowCash intValue]){
                self.collectionViewModel.contentArray = @[@"我的主页",@"今日任务",@"提现",@"设置",@"新手引导",@"意见反馈"];
            }
            [[NSUserDefaults standardUserDefaults] setObject:infModel.UserTags forKey:@"UserTags"];
            [[NSUserDefaults standardUserDefaults] setObject:infModel.Birthday forKey:@"Birthday"];
            [[NSUserDefaults standardUserDefaults] setObject:infModel.Balance forKey:@"Balance"];

        });
    }];
}

- (void)reloadTabItemBageCountNotifi{
    [self newMesageNotifi];
}

- (void)newMesageNotifi{
    NSArray *conversations = [[EMClient sharedClient].chatManager getAllConversations];
    NSInteger unreadCount = 0;
    for (EMConversation *conversation in conversations) {
        unreadCount += conversation.unreadMessagesCount;
    }
    unreadCount += [[HRFMDBDynamicNotifacationTool sharedInstance] unReadCount:nil];
    
    if (unreadCount){
        self.badgeView.hidden = NO;
    }else{
        self.badgeView.hidden = YES;
    }

}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self newMesageNotifi];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
